package com.alibaba.citrus.cr.mn.order.center.facade.function.common;

import cn.hutool.core.date.StopWatch;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.mn.order.center.facade.function.check.AbstractOrderParamsCheckService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.dataobject.AtomOrderDO;
import com.alibaba.cz.base.tool.log.Log;
import com.epoch.app.mnordercenter.model.dto.OrderAtomSubmitResponse;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomSubmitRequest;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangxiaorui
 * @ClassName OrderAtomSubmitService
 * @description: TODO
 * @date 2023/05/11 16:27
 * @version: 1.0
 */
public interface OrderAtomSubmitService {

    Log log = Log.getLogger(OrderAtomSubmitService.class);

    String CORE_STOP_WATCH_SEQ = "订单中心原子提交";

    /*指定单据类型 销售订单Tag：normal，退货订单：return，换货订单：exchange 等等*/
    String getOrderTag();

    AbstractOrderParamsCheckService getCheckService();

    /**
     * 销售订单提交订单核心处理逻辑（原子能力）
     *
     * @param orderList
     * @return
     */
    default List<OrderAtomSubmitResponse> batchSubmit(List<OrderAtomSubmitRequest> orderList) {
        StopWatch stopWatch = new StopWatch("OrderAtomSubmitService");
        try {
            List<OrderAtomSubmitResponse> responses = Lists.newArrayList();

            stopWatch.start("paramsInvalidCheck");
            paramsInvalidCheck(orderList, responses);
            stopWatch.stop();

            // 获取数据
            stopWatch.start("querySalesOrder");
            List<AtomOrderDO> orderSDOList = querySalesOrder(orderList);
            stopWatch.stop();

            // 业务校验
            stopWatch.start("checkBizInvalid");
            checkBizInvalid(orderSDOList, responses);
            stopWatch.stop();

            // 更新修改人姓名、编码 等信息
            stopWatch.start("refreshData");
            refreshData(orderSDOList, orderList);
            stopWatch.stop();

            // 事务条
            stopWatch.start("submitUpdateTransactional");
            submitUpdateTransactional(orderSDOList);
            stopWatch.stop();

            // 后置处理
            stopWatch.start("afterSubmitTransactional");
            afterSubmitTransactional(orderSDOList);
            stopWatch.stop();

            // 打印链路耗时日志
//            printExecConsumerTime();
            return responses;
        } catch (FunctionException ex) {
            log.error("OrderAtomCreateService#batchSubmit funcError", ex);
            throw ex;
        } catch (Exception e) {
            log.error("OrderAtomSubmitService#batchSubmit error", e);
            throw e;
        } finally {
            if (stopWatch.isRunning()) stopWatch.stop();
            log.info("OrderAtomSubmitService#batchSubmit cost time:{}", stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
        }
    }

    void paramsInvalidCheck(List<OrderAtomSubmitRequest> requestList, List<OrderAtomSubmitResponse> responseList);

    void printExecConsumerTime();

    void refreshData(List<AtomOrderDO> orderSDOList, List<OrderAtomSubmitRequest> orderList);

    /**
     * 后置处理
     *
     * @param orderSDOList 销售订单信息
     */
    void afterSubmitTransactional(List<AtomOrderDO> orderSDOList);

    /**
     * 获取销售订单信息
     *
     * @param orderRequestList
     * @return
     */
    List<AtomOrderDO> querySalesOrder(List<OrderAtomSubmitRequest> orderRequestList);

    /**
     * 更新
     *
     * @param orderSDOList
     */
    void submitUpdateTransactional(List<AtomOrderDO> orderSDOList);

    /**
     * 业务校验
     *
     * @param orderSDOList                  销售订单
     * @param salesOrderAtomSubmitResponses 返回异常信息
     */
    void checkBizInvalid(List<AtomOrderDO> orderSDOList, List<OrderAtomSubmitResponse> salesOrderAtomSubmitResponses);

}
